<?php
/*
分页类
arg
tbName 		表名
pageSzip	显示条数
lang 		语言
seach		查询条件值
order		排序条件
where		条件
col			查询条件的字段
now			当前所在位置
***********************/

function pageing($tbName='',$pageSize=10,$lang='',$seach='',$order='',$where='',$col='',$now='',$meeting){

	$get_param='';
	if($lang!=''){
		$get_param.="&lang={$lang}";
		$db_lang="_{$lang}";	
	}
	if($seach!=''){
		$get_param.="&seach={$seach}";
		$seach=" and {$col} like '%{$seach}%'";	
	}
	if($order!=''){
		if($tbName != 'audience')
			$get_param.="&order={$order}";
		else
			$get_param.='&order='.$_GET['order'].'&asc='.$_GET['asc'];
	}
	if($_GET['information_category']){
		$get_param.='&information_category='.$_GET['information_category'];	
	}
	if($_GET['category']){
		$get_param.='&category='.$_GET['category'];	
	}
	if($meeting!='')
		$model=M($tbName,'','DB_MEETING');
	else
		$model=M($tbName,'','');

	$sql = 'select count(*) as num from '.$tbName.' where '.$where.$seach;
	
	$r = $model->query($sql);
	
	$totalRow =$r[0][num];

	$totalPage= ceil($totalRow/$pageSize);
	
	$page=isset($_GET['page'])?$_GET['page']:1;
	$page=$page<1?$page=1:$page;
	$page=$page>$totalPage?$totalPage:$page;

	$first=($page-1)*$pageSize;

	$rs['data']=$model->where("{$where} {$seach}")->order("{$order}")->limit($first,$pageSize)->select();

	$now==''?'index':$now;
	$pageFor=__URL__."/".$now."?page=";
	
	if($page>1){
		$rs['show']="<li><a href='{$pageFor}1{$get_param}' title='First Page'>&laquo; 首页</a></li>
	  <li><a href='{$pageFor}".($page-1)."{$get_param}' title='Previous Page'>&laquo; 上一页</a></li>";	
	}
	$offset = $pagesize*($page-1);
	$init = 1;
	$max = $totalPage;
	$pagelen=9;
	$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
	$pageoffset = ($pagelen-1)/2;
	if($totalPage>$pagelen){
		if($page<=$pageoffset){
			$init=1;
			$max = $pagelen;
		}else{
			if($page+$pageoffset>=$totalPage+1){
				$init = $totalPage-$pagelen+1;
			}else{
				$init = $page-$pageoffset;
				$max = $page+$pageoffset;
			}
		}
	}
	//生成html
	for($i=$init;$i<=$max;$i++){
		$current=$i==$page?'current':'';
		$rs['show'].="
			<li><a href='{$pageFor}{$i}{$get_param}' class='number {$current}' title='{$i}'>{$i}</a></li>
		";
	}
	if($page<$totalPage){
		$rs['show'].="<li><a href='{$pageFor}".($page+1)."{$get_param}' title='Next Page'>下一页 &raquo;</a></li>
			  <li><a href='{$pageFor}{$totalPage}{$get_param}' title='Last Page'>尾页 &raquo;</a></li>
		";
	}
	return $rs;
}


function Database($cid){
	if(!isset($cid)){
		echo "<script>alert('请先登陆,再执行操作!');window.location='".__APP__."'</script>";
	}
	$cid = (int)$cid;
	$da=C('DOMAIN_TAG_NAME')."_".$cid;
	C('DB_MEETING.DB_NAME',$da);
	return true;
}

function page_ajax($tbName='',$pageSize=10,$lang='',$seach='',$order='',$where='',$col='',$now='',$meeting){
	$pageSize=$pageSize;
	$get_param='';
	if($lang!=''){
		$get_param.="&lang={$lang}";
		$db_lang="_{$lang}";	
	}
	if($seach!=''){
		$get_param.="&seach={$seach}";
		$seach="and {$col} like '%{$seach}%'";	
	}
	if($order!=''){
		$get_param.="&order={$order}";
	}
	if($_GET['information_category']){
		$get_param.='&information_category='.$_GET['information_category'];	
	}
	if($_GET['category']){
		$get_param.='&category='.$_GET['category'];	
	}
	if($meeting!='')
		$model=M("{$tbName}",'','DB_MEETING');
	else
		$model=M("{$tbName}");
	$totalRow=$model->where("{$where} {$seach}")->count();
	$totalPage=ceil($totalRow/$pageSize);
	$page=isset($_GET['page'])?$_GET['page']:1;
	$page=$page<1?$page=1:$page;
	$page=$page>$totalPage?$page=$totalPage:$page;
	$first=($page-1)*$pageSize;
	$rs['data']=$model->where("{$where} {$seach}")->order("{$order}")->limit($first,$pageSize)->select();
	$now==''?'index':$now;
	$pageFor=__URL__."/".$now."?page=";
	if($page>1){
		$rs['show']="<a class='first' name='{$totalPage}' title='First Page'>&laquo; 首页</a>
		
	  <a class='prev' title='Previous Page'>&laquo; 上一页</a> ";	
	}
	$offset = $pagesize*($page-1);
	$init = 1;
	$max = $totalPage;
	$pagelen=9;
	$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
	$pageoffset = ($pagelen-1)/2;
	if($totalPage>$pagelen){
		if($page<=$pageoffset){
			$init=1;
			$max = $pagelen;
		}else{
			if($page+$pageoffset>=$totalPage+1){
				$init = $totalPage-$pagelen+1;
			}else{
				$init = $page-$pageoffset;
				$max = $page+$pageoffset;
			}
		}
	}
	//生成html
	for($i=$init;$i<=$max;$i++){
		$current=$i==$page?'current':'';
		$rs['show'].="
			<a class='number {$current}' title='{$i}'>{$i}</a>
		";
	}
	if($page<$totalPage){
		$rs['show'].="<a class='next' title='Next Page'>下一页 &raquo;</a>
			  <a class='last' name='{$totalPage}' title='Last Page'>尾页 &raquo;</a>
		";
	}
	return $rs;
}

function page_room($tbName='',$pageSize=10,$lang='',$seach='',$order='',$where='',$col='',$now='',$meeting,$a){
		$pageSize=$pageSize;
		$get_param='';
		if($lang!=''){
			$get_param.="&lang={$lang}";
			$db_lang="_{$lang}";	
		}
		if($seach!=''){
			$get_param.="&seach={$seach}";
			for($i=0;$i<count($col);$i++){
				if($col[$i]==$a){
					foreach($seach[$col[$i]] as $k=>$v){
						$seat[] = array($k,$v);
					}
					$seachs[$col[$i]] = $seat;
				}else{
					$seachs[$col[$i]]=array("like","%{$seach[$col[$i]]}%");
				}
			}				
		}
		
		if($order!=''){
			$get_param.="&order={$order}";
		}
		if($_GET['information_category']){
			$get_param.='&information_category='.$_GET['information_category'];	
		}
		if($_GET['category']){
			$get_param.='&category='.$_GET['category'];	
		}
		if($meeting!='')
			$model=M("{$tbName}",'','DB_MEETING');
		else
			$model=M("{$tbName}");
	
		$totalRow=$model->where($seachs)->count();

		$totalPage=ceil($totalRow/$pageSize);
		$page=isset($_GET['page'])?$_GET['page']:1;
		$page=$page<1?$page=1:$page;
		$page=$page>$totalPage?$page=$totalPage:$page;

		$first=($page-1)*$pageSize;
		$rs['data']=$model->where($seachs)->order("{$order}")->limit($first,$pageSize)->select();
		$now==''?'index':$now;
		$pageFor=__URL__."/".$now."?page=";
		if($page>1){
			$rs['show']="<li><a href='{$pageFor}1{$get_param}' title='First Page'>&laquo; 首页</a></li>
		  <li><a href='{$pageFor}".($page-1)."{$get_param}' title='Previous Page'>&laquo; 上一页</a></li>";
		  
		}
		$offset = $pagesize*($page-1);
		$init = 1;
		$max = $totalPage;
		$pagelen=9;
		$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
		$pageoffset = ($pagelen-1)/2;
		if($totalPage>$pagelen){
			if($page<=$pageoffset){
				$init=1;
				$max = $pagelen;
			}else{
				if($page+$pageoffset>=$totalPage+1){
					$init = $totalPage-$pagelen+1;
				}else{
					$init = $page-$pageoffset;
					$max = $page+$pageoffset;
				}
			}
		}
		//生成html
		for($i=$init;$i<=$max;$i++){
			$current=$i==$page?'current':'';
			$rs['show'].="
				<li><a href='{$pageFor}{$i}{$get_param}' class='number {$current}' title='{$i}'>{$i}</a></li>
			";
		}
		if($page<$totalPage){
			$rs['show'].="<li><a href='{$pageFor}".($page+1)."{$get_param}' title='Next Page'>下一页 &raquo;</a></li>
				  <li><a href='{$pageFor}{$totalPage}{$get_param}' title='Last Page'>尾页 &raquo;</a></li>
			";
		}
		return $rs;
}

function page_array_ajax($tbName='',$pageSize=10,$lang='',$seach='',$order='',$where='',$col='',$now='',$meeting,$a){
		$pageSize=$pageSize;
		$get_param='';
		if($lang!=''){
			$get_param.="&lang={$lang}";
			$db_lang="_{$lang}";	
		}
		if($seach!=''){
			$get_param.="&seach={$seach}";
			for($i=0;$i<count($col);$i++){
				if($col[$i]==$a){
					foreach($seach[$col[$i]] as $k=>$v){
						$seat[] = array($k,$v);
					}
					$seachs[$col[$i]] = $seat;
				}else{
					$seachs[$col[$i]]=array("like","%{$seach[$col[$i]]}%");
				}
			}				
		}
		
		if($order!=''){
			$get_param.="&order={$order}";
		}
		if($_GET['information_category']){
			$get_param.='&information_category='.$_GET['information_category'];	
		}
		if($_GET['category']){
			$get_param.='&category='.$_GET['category'];	
		}
		if($meeting!='')
			$model=M("{$tbName}",'','DB_MEETING');
		else
			$model=M("{$tbName}");
	
		$totalRow=$model->where($seachs)->count();
		
		$totalPage=ceil($totalRow/$pageSize);
		$page=isset($_GET['page'])?$_GET['page']:1;
		$page=$page<1?$page=1:$page;
		$page=$page>$totalPage?$page=$totalPage:$page;
		$first=($page-1)*$pageSize;
		$rs['data']=$model->where($seachs)->order("{$order}")->limit($first,$pageSize)->select();
		$now==''?'index':$now;
		$pageFor=__URL__."/".$now."?page=";
		if($page>1){
			$rs['show']="<li><a class='first' name='{$totalPage}' title='First Page'>&laquo; 首页</a></li>
		  <li><a class='prev' title='Previous Page'>&laquo; 上一页</a></li>";	
		}
		$offset = $pagesize*($page-1);
		$init = 1;
		$max = $totalPage;
		$pagelen=9;
		$pagelen = ($pagelen%2)?$pagelen:$pagelen+1;
		$pageoffset = ($pagelen-1)/2;
		if($totalPage>$pagelen){
			if($page<=$pageoffset){
				$init=1;
				$max = $pagelen;
			}else{
				if($page+$pageoffset>=$totalPage+1){
					$init = $totalPage-$pagelen+1;
				}else{
					$init = $page-$pageoffset;
					$max = $page+$pageoffset;
				}
			}
		}
		//生成html
		for($i=$init;$i<=$max;$i++){
			$current=$i==$page?'current':'';
			$rs['show'].="
				<li><a class='number {$current}' title='{$i}'>{$i}</a></li>
			";
		}
		if($page<$totalPage){
			$rs['show'].="<li><a class='next' title='Next Page'>下一页 &raquo;</a></li>
				  <li><a class='last' name='{$totalPage}' title='Last Page'>尾页 &raquo;</a></li>
			";
		}
		return $rs;
}



?>